// Created by liusc on 2020/9/17-9:24.
import {getDict} from "~/utils";
import {getInitObj, addObj} from "@/views/wy-budget/api/budgetadd.js";

export default {
    name: "addition_add",
    data() {
        return {
            dialogVisible: false,
            isEdit: false,

            form: {
                applyTime: [],
                budgetId: '',
                budgetCode: '',
                applyName: '',
                applyDept: '',
                templateName: '',
                applyStart: '',
                applyEnd: '',
                budgetType: '13',
                remarks: '',
            },

            rules: {
                budgetCode: [
                    {required: true, message: '请输入预算编号', trigger: 'blur'},
                ],
                applyName: [
                    {required: true, message: '请输入申请名称', trigger: 'blur'},
                ],
                applyDept: [
                    {required: true, message: '请选择部门', trigger: 'change'},
                ],
                templateName: [
                    {required: true, message: '请输入模板', trigger: 'blur'},
                ],
                applyTime: [
                    {required: true, message: '请选择申请时间', trigger: 'change '},
                ],
                remarks: [
                    {required: true, message: '请输入备注', trigger: 'blur'},
                ],
            },
            //防止重复提交
            canSubmit: true,
        }
    },

    mounted() {
    },

    created() {
        this.lists = getDict('budget_department');
    },

    methods: {
        daterangeChange(e){
            let _this =this
            _this.$nextTick(() => {
                _this.$set(_this.form, "applyTime", [e[0], e[1]]);
                _this.$forceUpdate();
            });
        },

        show() {
            const loading = this.$loading({
                lock: true,
                text: '数据处理中'
            });

            this.dialogVisible = true
            this.isEdit = false

            setTimeout(() => {
                this.$refs['add-form'].resetFields()

                getInitObj().then(resp => {
                    if (resp.data.data) {
                        this.form.budgetCode = resp.data.data.budgetCode
                        this.form.templateName = resp.data.data.templateName
                    }
                    loading.close()
                }).catch(() => {
                    loading.close()
                })
            }, 100)
        },

        commit() {
            const loading = this.$loading({
                lock: true,
                text: '数据处理中'
            });

            this.$refs['add-form'].validate(valid => {
                if (valid) {
                    var startt=this.dataFormat(this.form.applyTime[0],'yyyy-MM-dd')
                    var endt=this.dataFormat(this.form.applyTime[1],'yyyy-MM-dd')

                    let dept = this.form.applyDept

                    this.form.id = new Date().getTime()
                    this.form.applyStart=startt
                    this.form.applyEnd=endt
                    this.form.applyDept = this.form.applyDept.join(',')

                    let params = Object.assign({},this.form)
                    this.form.applyDept = dept
                    if(this.canSubmit){
                        this.canSubmit = false
                        addObj(params).then(resp => {
                            if (resp.data.data) {
                                this.suc('数据提交成功')
                                this.closeDialog()
                                this.$emit("query")
                            }
                            loading.close()
                        }).catch(() => {
                            loading.close()
                        }).finally(() => {
                            this.canSubmit = true
                        })
                    }

                    // this.$emit('addData', params)
                    // this.dialogVisible = false
                }else{
                    loading.close()
                }
            })
        },

        dataFormat(value, fmt) {
            let getDate = new Date(value);
            let o = {
                'M+': getDate.getMonth() + 1,
                'd+': getDate.getDate(),
                'h+': getDate.getHours(),
                'm+': getDate.getMinutes(),
                's+': getDate.getSeconds(),
                'q+': Math.floor((getDate.getMonth() + 3) / 3),
                'S': getDate.getMilliseconds()
            };
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
            }
            for (let k in o) {
                if (new RegExp('(' + k + ')').test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                }
            }
            return fmt;
        },

        closeDialog(){
            this.form = {
                applyTime: [],
                budgetId: '',
                budgetCode: '',
                applyName: '',
                applyDept: '',
                templateName: '',
                applyStart: '',
                applyEnd: '',
                remarks: '',
            },
            this.dialogVisible = false
        },
    }
}
